"""Build tests"""


def build_dts_library(env, dc_credit):
    """Build libdts library"""

    denv = env.Clone()

    denv.require('argobots', 'protobufc')

    denv.d_static_library('dts', [dc_credit, 'dts.c'])


def build_tests(env, prereqs):
    """build the tests"""
    Import('libdaos_tgts', 'cmd_parser')
    denv = env.Clone()
    denv.compiler_setup()

    libs_server = ['dts', 'daos_tests', 'daos_common_pmem', 'cart', 'gurt', 'uuid', 'pthread',
                   'dpar', 'isal', 'protobuf-c', 'cmocka']
    libs_client = ['dts', 'daos_tests', 'daos', 'daos_common', 'daos_tests', 'gurt', 'cart', 'uuid',
                   'pthread', 'dpar', 'cmocka']

    denv.AppendUnique(CPPPATH=[Dir('suite').srcnode()])
    denv.AppendUnique(LIBPATH=[Dir('.')])
    denv.require('isal', 'protobufc')

    daos_racer = denv.d_program('daos_racer', ['daos_racer.c'], LIBS=libs_client)
    denv.Install('$PREFIX/bin/', daos_racer)

    perf_common = denv.StaticObject(['perf_common.c'])

    daos_perf = denv.d_program('daos_perf', ['daos_perf.c', perf_common], LIBS=libs_client)
    denv.Install('$PREFIX/bin/', daos_perf)

    if prereqs.server_requested():
        tenv = denv.Clone()
        tenv.require('argobots', 'pmdk')

        libs_server += ['vos', 'bio', 'abt', 'numa']
        vos_engine = tenv.StaticObject(['vos_engine.c'])

        vos_perf = tenv.d_program('vos_perf',
                                  ['vos_perf.c', perf_common, vos_engine] + libdaos_tgts,
                                  LIBS=libs_server)
        tenv.Install('$PREFIX/bin/', vos_perf)

        obj_ctl = tenv.d_program('obj_ctl', ['obj_ctl.c', cmd_parser, vos_engine] + libdaos_tgts,
                                 LIBS=libs_server)
        tenv.Install('$PREFIX/bin/', obj_ctl)

    jobtest = denv.d_program('jobtest', ['jobtest.c'], LIBS=libs_client)
    denv.Install('$PREFIX/bin/', jobtest)

    # tests
    SConscript('suite/SConscript', exports=['denv'])

    # Build drpc_test
    SConscript('drpc/SConscript')

    # Build security_test
    SConscript('security/SConscript')

    # ftest
    SConscript('ftest/SConscript')

    # rpc tests
    SConscript('rpc/SConscript')

    # daos middleware consistency corruption tool for testing
    libs_client += ['dfs']
    denv.AppendUnique(RPATH_FULL=['$PREFIX/lib64'])
    daos_mw = denv.d_program('daos_mw_fi', 'daos_mw_fi.c', LIBS=libs_client, RPATH='$PREFIX/lib64')
    denv.Install('$PREFIX/lib/daos/TESTING/tests', daos_mw)


def scons():
    """Execute build"""
    Import('env', 'base_env', 'base_env_mpi', 'prereqs', 'dc_credit')

    if not prereqs.test_requested():
        return

    build_dts_library(env, dc_credit)

    denv = base_env.Clone()
    denv.AppendUnique(LIBPATH=[Dir('../gurt')])
    denv.AppendUnique(LIBPATH=[Dir('../common')])
    denv.AppendUnique(LIBPATH=[Dir('../client/api')])
    denv.AppendUnique(LIBPATH=[Dir('../cart')])
    denv.AppendUnique(LIBPATH=[Dir('../vos')])
    denv.AppendUnique(LIBPATH=[Dir('../bio')])
    denv.AppendUnique(LIBPATH=[Dir('../utils/wrap/mpi')])
    # Add runtime paths for daos libraries
    denv.AppendUnique(RPATH_FULL=['$PREFIX/lib64/daos_srv'])
    denv.AppendUnique(CPPPATH=[Dir('../mgmt').srcnode()])
    build_tests(denv, prereqs)

    if not base_env_mpi:
        return
    denv = base_env_mpi.Clone()

    denv.AppendUnique(LIBPATH=[Dir('../gurt')])
    denv.AppendUnique(LIBPATH=[Dir('../common')])
    denv.AppendUnique(LIBPATH=[Dir('../client/api')])
    denv.AppendUnique(LIBPATH=[Dir('../cart')])
    denv.AppendUnique(LIBPATH=[Dir('../client/dfs')])

    libs_client = ['daos_tests', 'daos', 'daos_common', 'cart', 'gurt', 'uuid', 'dfs']

    denv.d_program('simple_array', 'simple_array.c', LIBS=libs_client)
    denv.d_program('simple_obj', 'simple_obj.c', LIBS=libs_client)
    denv.d_program('simple_dfs', 'simple_dfs.c', LIBS=libs_client)


if __name__ == "SCons.Script":
    scons()
